﻿using System.Windows.Controls;
using QDP.Platform.Services;
using System.Linq;
namespace QDP.Platform.Loader
{
    public partial class MasterPanel : UserControl, IMasterService
    {
        public MasterPanel()
        {
            InitializeComponent();
            this.SizeChanged += (s, e) =>
            {
                RightRoot.Height = e.NewSize.Height - 30;
            };
            this.Loaded += MasterPanel_Loaded;
        }

        void MasterPanel_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            this.OnRequestConfigComplated();
        }

        private void OnRequestConfigComplated()
        {
            D.Debug("启动UI");
            // 获取全部自动启动的应用
            var autoruns = from p in Q.WidgetManager.Widgets
                           where p.Configs.ContainsKey("Autorun")
                              && p.Configs["Autorun"].ToLower() == "true"
                           && p.Navigation is QDP.Platform.ObjService.NavUISLXap
                           select p;

            D.Debug("启动自动运行的Apps");
            // 获取配置文件后加载组件
            D.LoadParts(autoruns.ToList(), actionArgs =>
            {
                // 处理加载组件时的各种状态
                switch (actionArgs.State)
                {
                    case LoadActionState.LoadComplated:
                        // 组件下载完成时触发，若成功加载，则打开组件，否则不做任何处理
                        if (actionArgs.Element != null)
                        {
                            actionArgs.Element.Show();
                            D.Debug(actionArgs.Navigation.Caption + "启动完成.");
                        }
                        break;
#if DEBUG
                    case LoadActionState.Error:
                        D.Debug("err:"+actionArgs.Navigation.Caption + "："  + actionArgs.Error.Message);
                        this.ShowMessage(actionArgs.Navigation.Caption,actionArgs.Error.ToString());
                        break;
#endif
                }
            });
        }

        #region 关于 IMasterService
        /// <summary>
        /// 
        /// </summary>
        public Panel Root { get { return this.LayoutRoot; } }
        /// <summary>
        /// 
        /// </summary>
        public Panel ContentRoot { get { return this._ContentRoot; } }
        /// <summary>
        /// 
        /// </summary>
        public Panel BottomRoot { get { return this._BottomRoot; } }
        /// <summary>
        /// 
        /// </summary>
        public Panel TopRoot { get { return this._TopRoot; } }
        /// <summary>
        /// 
        /// </summary>
        public Panel LeftRoot { get { return this._LeftRoot; } }
        /// <summary>
        /// 
        /// </summary>
        public Panel RightRoot { get { return this._RightRoot; } }
        #endregion //#region 关于 IMasterService
    }
}